package com.heima.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.search.service.ArticleSearchService;
import com.rabbitmq.tools.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ArticleSearchServiceImpl implements ArticleSearchService {
    private final RestHighLevelClient restHighLevelClient;
    @Override
    public ResponseResult search(UserSearchDto dto) throws IOException {
        SearchRequest searchRequest=new SearchRequest("app_info_article");
        //总体
        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
        //复杂查询构建
        BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
        //关键字查询
        QueryStringQueryBuilder queryStringQueryBuilder= QueryBuilders.queryStringQuery(dto.getSearchWords()).field("title").field("content").defaultOperator(Operator.OR);
boolQueryBuilder.must(queryStringQueryBuilder);
//时间范围查询，查询小于指定时间的
        RangeQueryBuilder rangeQueryBuilder=QueryBuilders.rangeQuery("publishTime").lt(dto.getMinBehotTime());
        boolQueryBuilder.filter(rangeQueryBuilder);
        //分页查询
        searchSourceBuilder.from(dto.getFromIndex());
        searchSourceBuilder.size(dto.getPageSize());
        searchRequest.source(searchSourceBuilder);
        //查询
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits searchHits = search.getHits();
        SearchHit[] hits = searchHits.getHits();
        List<Map> res=new ArrayList<>();
        for (SearchHit hit : hits) {
          // 获取json文档//转化为map
            String sourceAsString = hit.getSourceAsString();
            Map map = JSON.parseObject(sourceAsString, Map.class);
//处理高亮
          if(hit.getHighlightFields()!=null&&hit.getHighlightFields().size()>0){
              //处理高亮
              //获取title字段的高亮内容
              Text[] fragments = hit.getHighlightFields().get("title").getFragments();//获得标题高亮处理
              String join = StringUtils.join(fragments);//转化为字符串
              map.put("h_title",join);
          }else{
              map.put("title",map.get("title"));
            }

        }

        return new ResponseResult().ok(res);

    }
}
